<?php

function restaurant_restaurant_upload_page(){
    return drupal_render(drupal_get_form('restaurant_restaurant_upload_form'));
}

function restaurant_restaurant_upload_form($form,$form_state){
    if (isset($form_state['build_info']['args'][0])){
        $restaurant = ($form_state['build_info']['args'][0]);
    }else $restaurant=false;
    $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name:'),
        '#size' => 30,
        '#maxlength' => 64,
        "#default_value"=>$restaurant?$restaurant->name:''
    );
    $form['city'] = array(
        '#type' => 'textfield',
        '#title' => t('City:'),
        '#size' => 30,
        '#maxlength' => 64,
        "#default_value"=>$restaurant?$restaurant->city:''
    );
    $form['website'] = array(
        '#type' => 'textfield',
        '#title' => t('Website:'),
        '#size' => 30,
        '#maxlength' => 64,
        "#default_value"=>$restaurant?$restaurant->website:''
    );
    $form['telephone'] = array(
        '#type' => 'textfield',
        '#title' => t('Telephone:'),
        '#size' => 30,
        '#maxlength' => 64,
        "#default_value"=>$restaurant?$restaurant->telephone:''
    );
    $form['open'] = array(
        '#type' => 'textfield',
        '#title' => t('Open:'),
        '#size' => 30,
        '#maxlength' => 64,
        "#default_value"=>$restaurant?$restaurant->open:''
    );
    $form['delivery'] = array(
        '#type'=> 'checkbox',
        '#title' =>t('Delivery'),
        "#default_value"=>$restaurant?$restaurant->delivery:''
    );
    $form['wifi'] = array(
        '#type'=> 'checkbox',
        '#title' =>t('Wifi'),
        "#default_value"=>$restaurant?$restaurant->wifi:''
    );
    $form['parking'] = array(
        '#type'=> 'checkbox',
        '#title' =>t('Parking'),
        "#default_value"=>$restaurant?$restaurant->parking:''
    );
    $form['rid'] = array(
        '#type'=> 'hidden',
        "#default_value"=>$restaurant?$restaurant->rid:''
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
    );    
    
    return $form;
}

function restaurant_save_restaurant($restaurant){
  if($restaurant->rid==''){
    db_insert('ep_restaurants')
        ->fields(array(
            'uid'=>$restaurant->uid,
            'name' => $restaurant->name,
            'city' => $restaurant->city,
            'website' => $restaurant->website,
            'telephone' =>$restaurant->telephone,
            'open'=>$restaurant->open,
            'delivery'=>$restaurant->delivery,
            'wifi'=>$restaurant->wifi,
            'parking'=>$restaurant->parking, 
          ))
        ->execute(); 
  }else
      db_update('ep_restaurants')
        ->fields(array(
            'name' => $restaurant->name,
            'city' => $restaurant->city,
            'website' => $restaurant->website,
            'telephone' =>$restaurant->telephone,
            'open'=>$restaurant->open,
            'delivery'=>$restaurant->delivery,
            'wifi'=>$restaurant->wifi,
            'parking'=>$restaurant->parking, 
          ))
          ->condition('rid', $restaurant->rid,'=')
        ->execute(); 
}

function restaurant_restaurant_upload_form_submit($form, &$form_values){
       $restaurant = new stdClass();
       global $user;
       $restaurant->uid=$user->uid;
       $restaurant->rid=$form_values['values']['rid'];
       $restaurant->name=$form_values['values']['name'];
       $restaurant->city= $form_values['values']['city'];
       $restaurant->website= $form_values['values']['website'];
       $restaurant->telephone= $form_values['values']['telephone'];
       $restaurant->open= $form_values['values']['open'];
       $restaurant->delivery= $form_values['values']['delivery'];
       $restaurant->wifi= $form_values['values']['wifi'];
       $restaurant->parking= $form_values['values']['parking']; 
       restaurant_save_restaurant($restaurant);
       drupal_goto('restaurant/etterem_list');
}